package com.tcode.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.catalina.User;
import org.codehaus.jackson.map.ObjectMapper;

import com.tcode.bean.UserBean;
import com.tcode.service.UserService;
import com.tcode.utils.BeanFactory;
import com.tcode.utils.Page;
import com.tcode.utils.WriterUtil;

/**
 * Servlet implementation class InitServlet
 */
@WebServlet({"/InitServlet","/UserServlet"})
public class InitServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	UserService us = (UserService) BeanFactory.getBean("UserService");
//	UserService us = new UserService();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public InitServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    
    public void login(HttpServletRequest request, HttpServletResponse response){
    	String username =request.getParameter("userName");
		String pwd =request.getParameter("password");
		
		UserBean user = new UserBean();
		user.setName(username);
		user.setPhone(pwd);
		
		try {
			if(us.getUserCount(user)){
				request.setAttribute("username", username);
				request.getSession().setAttribute("user", us.getUserInfo(user));
				request.getRequestDispatcher("second.jsp").forward(request, response);
			}else{
				request.setAttribute("errorTip", "用户名密码有误");
				request.getRequestDispatcher("index.jsp").forward(request, response);
			}
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
//			request.getRequestDispatcher("404.jsp").forward(request, response);
		}
    }
    
    public void register(HttpServletRequest request, HttpServletResponse response) throws IOException{
    	String userName =request.getParameter("username").trim();
    	String pwd =request.getParameter("password").trim();
    	UserBean u =new UserBean();
    	u.setName(userName);
    	u.setPhone(pwd);
    	ObjectMapper om = WriterUtil.writeTopage();
    	Map<String, String> map = new HashMap<String,String>();
		try {
			if(us.addUser(u)){
				map.put("flag", "1");
			}else{
				map.put("flag", "0");
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put("flag", "-1");
		}
		om.writeValue(response.getOutputStream(), map);
		
	}
    
    
    public void getAllUser(HttpServletRequest request, HttpServletResponse response) throws Exception{
    	
    	int pageNo =Integer.parseInt(request.getParameter("pageNo").trim());
    	int pageSize =Integer.parseInt(request.getParameter("pageSize").trim());
    	
    	UserBean u = new UserBean();
    	
    	Page<UserBean> pages =us.getUserList(u, pageNo, pageSize);
    	ObjectMapper om = WriterUtil.writeTopage();
    	Map<String, Object> map = new HashMap<String,Object>();
    	map.put("msg", pages);
    	
    	om.writeValue(response.getOutputStream(), map);
    	
    }
    
    public void changeUser(HttpServletRequest request, HttpServletResponse response)throws Exception{
    	String userName =request.getParameter("username").trim();
    	String pwd =request.getParameter("password").trim();
    	String id =request.getParameter("id").trim();
    	String deptid =request.getParameter("deptid").trim();
    	UserBean u =new UserBean();
    	u.setId(Integer.parseInt(id));
    	u.setDeptId(Integer.parseInt(deptid));
    	u.setName(userName);
    	u.setPhone(pwd);
    	ObjectMapper om = WriterUtil.writeTopage();
    	Map<String, String> map = new HashMap<String,String>();
		try {
			if(us.updateUser(u)){
				map.put("flag", "1");
			}else{
				map.put("flag", "0");
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put("flag", "-1");
		}
		om.writeValue(response.getOutputStream(), map);
    }
    
    
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		// TODO Auto-generated method stub
		String method =request.getParameter("_method");
		if("login".equals(method)){
			login(request, response);
		}else if("register".equals(method)){
			register(request, response);
		}else if("getAllUser".equals(method)){
			try {
				getAllUser(request, response);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else{
			try {
				changeUser(request, response);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
